

#import "CalendarListTableViewCell.h"

@interface CalendarListTableViewCell()
- (UILabel *)newLabelWithPrimaryColor:(UIColor *)primaryColor selectedColor:(UIColor *)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold;
@end

@implementation CalendarListTableViewCell

@synthesize calendarSubjectLabel = _calendarSubjectLabel;
@synthesize calendarLocationLabel = _calendarLocationLabel;
@synthesize calendarBeginTimeLabel = _calendarBeginTimeLabel;


+ (void)initialize
{
	
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView *myContentView = self.contentView;
        
		
		self.calendarBeginTimeLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:14.0 bold:YES]; 
		self.calendarBeginTimeLabel.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.calendarBeginTimeLabel];
		[self.calendarBeginTimeLabel release];
        
        self.calendarSubjectLabel = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:16.0 bold:YES]; 
		self.calendarSubjectLabel.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.calendarSubjectLabel];
		[self.calendarSubjectLabel release];

        self.calendarLocationLabel = [self newLabelWithPrimaryColor:[UIColor darkGrayColor] selectedColor:[UIColor whiteColor] fontSize:14.0 bold:NO];
		self.calendarLocationLabel.textAlignment = UITextAlignmentLeft;
		[myContentView addSubview:self.calendarLocationLabel];
		[self.calendarLocationLabel release];
        
    }
    return self;
}

- (Calendar *)calendar
{
    return _calendar;
}


- (void)setCalendar:(Calendar *)newCalendar
{
    [newCalendar retain];
    [_calendar release];
    _calendar = newCalendar;	
	NSString* subject=newCalendar.subject;
	if(newCalendar.meetingType==12)
		subject=[NSString stringWithFormat:@"%@(%@)", subject, @"P"];
	else if(newCalendar.meetingType==206)
		subject=[NSString stringWithFormat:@"%@(%@)", subject, @"C"];
    self.calendarSubjectLabel.text = subject;
	if(newCalendar.beginTime!=nil)
	{

		NSRange range={11, 8};
		NSMutableString* today=[[NSMutableString alloc] init];
		[today setString:[[NSDate date] description]];
		[today replaceCharactersInRange:range withString:@"00:00:00"];
		NSDate* todayOffset=[[NSDate alloc] initWithString:today];
		NSTimeInterval interval=[todayOffset timeIntervalSinceDate:newCalendar.beginTime]+24*60*60; 
		if(0<interval&&interval<(60*24*60)){
			self.calendarBeginTimeLabel.textColor=[UIColor blueColor];
		}
	
		NSDateFormatter* transfer=[[NSDateFormatter alloc] init];
		[transfer setDateFormat:@"H:mm"];
		self.calendarBeginTimeLabel.text=[transfer stringFromDate:newCalendar.beginTime];
		[transfer release];
		[today release];
		[todayOffset release];
	}
    self.calendarLocationLabel.text = newCalendar.location;
	[self setNeedsDisplay];
}




- (void)layoutSubviews {
    

#define UPPER_ROW_OFFSET 6
#define LOWER_ROW_OFFSET 24
#define LEFT_OFFSET 3
#define TIME_WIDTH 40
#define ROW_HEIGHT 18		
#define	SUBJECT_WIDTH 270
#define COLUMN_OFFSET 5
#define LOCATION_WIDTH 270

    
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
	
	if (!self.editing) {
		
        CGFloat boundsX = contentRect.origin.x + 20;
		CGRect frame;
		//THE TIME
		frame = CGRectMake(LEFT_OFFSET+boundsX , UPPER_ROW_OFFSET, TIME_WIDTH, ROW_HEIGHT);
		self.calendarBeginTimeLabel.frame = frame;
		
		//THE SUBJECT
		frame = CGRectMake(boundsX + LEFT_OFFSET+COLUMN_OFFSET+TIME_WIDTH, UPPER_ROW_OFFSET, SUBJECT_WIDTH, ROW_HEIGHT);
		self.calendarSubjectLabel.frame = frame;
	
        
        // THE LOCATION
        frame = CGRectMake(boundsX + LEFT_OFFSET+COLUMN_OFFSET+TIME_WIDTH, LOWER_ROW_OFFSET, LOCATION_WIDTH, ROW_HEIGHT);
        self.calendarLocationLabel.frame = frame;
		
	}
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
	/*
	 Views are drawn most efficiently when they are opaque and do not have a clear background, so in newLabelForMainText: the labels are made opaque and given a white background.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  
     */
	[super setSelected:selected animated:animated];
	
	UIColor *backgroundColor = nil;
	if (selected) {
	    backgroundColor = [UIColor clearColor];
	} else {
		backgroundColor = [UIColor whiteColor];
	}
    
	
	self.calendarSubjectLabel.backgroundColor = backgroundColor;
	self.calendarSubjectLabel.highlighted = selected;
	self.calendarSubjectLabel.opaque = !selected;
	
		
	self.calendarBeginTimeLabel.backgroundColor = backgroundColor;
	self.calendarBeginTimeLabel.highlighted = selected;
	self.calendarBeginTimeLabel.opaque = !selected;
	
	self.calendarLocationLabel.backgroundColor = backgroundColor;
	self.calendarLocationLabel.highlighted = selected;
	self.calendarLocationLabel.opaque = !selected;
}

- (UILabel *)newLabelWithPrimaryColor:(UIColor *)primaryColor selectedColor:(UIColor *)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold
{
	/*
        Create and configure a label.
    */

    UIFont *font;
    if (bold) {
        font = [UIFont boldSystemFontOfSize:fontSize];
    } else {
        font = [UIFont systemFontOfSize:fontSize];
    }
    
    /*
        Views are drawn most efficiently when they are opaque and do not have a clear background, so set these defaults.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  This is handled in setSelected:animated:.
    */
	UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
	newLabel.backgroundColor = [UIColor whiteColor];
	newLabel.opaque = YES;
	newLabel.textColor = primaryColor;
	newLabel.highlightedTextColor = selectedColor;
	newLabel.font = font;
	
	return newLabel;
}




@end
